#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 30 jun 2011

@author: Joakim Lindskog

Copyright (c) 2011 Joakim Lindskog

This file is part of MyGarden.

    MyGarden is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    MyGarden is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with MyGarden.  If not, see <http://www.gnu.org/licenses/>.
'''
from gardenbase import gardensprite
import game, util, events

class Icon(gardensprite.GardenSprite):
    '''
    Icons for clicking on
    '''
    icon_sound = None

    def __init__(self, name, image, action):
        '''
        Constructor
        '''
        super(Icon, self).__init__(image)
        self.name = name
        self.action = action
        self.ref = None
        self.tooltip_text = _("missing_tooltip")
        if not Icon.icon_sound:
            Icon.icon_sound = util.load_effect('tock.ogg')
        self.type = None
        
    def on_mouse_press(self, x, y, button, modifiers):
        if self.is_clicked(x, y, button, modifiers):
            # Do action
            if self.ref != None:
                self.action(self.ref, x, y)
            else:
                self.action(x, y)
            # Play icon sound
            Icon.icon_sound.play()
            # Dispatch click event
            events.EventBus().dispatch_event(events.IconClickEvent(self, x, y, type=self.type))
            return True
        return False
    
    def on_mouse_motion(self, x, y, dx, dy):
        if self.is_over(x, y):
            game.Game().gui_layer.showTooltip(self, x, y)
            return True
        return False